package edu.scau.sku.service;

import edu.scau.core.Enum.ColorEnum;
import edu.scau.core.Enum.SizeEnum;
import edu.scau.core.util.ServletUtils;
import edu.scau.sku.domain.SkuColorSize;
import edu.scau.sku.mapper.SkuColorSizeMapper;
import edu.scau.sku.mapper.SkuMapper;
import edu.scau.sku.vo.SkuColorSizeVo;
import edu.scau.system.domain.LoginUser;
import edu.scau.system.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class SkuColorSizeServiceImpl implements SkuColorSizeService{
    @Autowired
    private SkuColorSizeMapper skuColorSizeMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SkuMapper skuMapper;

    @Override
    public List<SkuColorSizeVo> listAll() {
        return skuColorSizeMapper.listAll();
    }

    @Override
    public SkuColorSizeVo findSkuColorSizeByCode(String skuCode) {
        return skuColorSizeMapper.findSkuColorSizeByCode(skuCode);
    }

    @Override
    public List<SkuColorSizeVo> listByPage(SkuColorSizeVo skuColorSizevo) {
        return skuColorSizeMapper.listByPage(skuColorSizevo);
    }

    @Override
    public int updateSkuColorSize(SkuColorSizeVo skuColorSizeVo) {
        if(skuColorSizeVo.getSkuCode().isEmpty()){
            throw new RuntimeException("库存编号不存在！");
        }
        if(!checkUnique(skuColorSizeVo.getSkuCode())){
            throw new RuntimeException("库存不存在！");
        }
        Date updateTime=new Date();
        LoginUser loginUser=tokenService.getLoginUser(ServletUtils.getRequest());
        String updateBy=loginUser.getUsername();
        SkuColorSize skuColorSize=new SkuColorSize(skuColorSizeVo);
        skuColorSize.setUpdateBy(updateBy);
        skuColorSize.setUpdateTime(updateTime);
        return skuColorSizeMapper.updateSkuColorSize(skuColorSize);
    }

    @Override
    @Transactional
    public int deleteSkuColorSizeByCode(String skuCode) {
        int res =  skuColorSizeMapper.deleteSkuColorSizeByCode(skuCode);
        //检查并删除对应库存
        if(skuService.isExisted(skuCode)){
            int del = skuService.deleteSkuByCode(skuCode);
        }
        return res;
    }

    @Override
    public int insertSkuColorSize(SkuColorSizeVo skuColorSizeVo) throws IllegalArgumentException{
        if(skuColorSizeVo.getSkuCode() == null || skuColorSizeVo.getSkuCode().isEmpty()){
            throw new DataIntegrityViolationException("库存编号不存在！");
        }
        if(checkUnique(skuColorSizeVo.getSkuCode())){
            throw new DataIntegrityViolationException("该库存编号已存在");
        }
        //判断颜色
        if(!skuColorSizeVo.getSize().isEmpty()){
            SizeEnum.fromString(skuColorSizeVo.getSize());
        }
        if(!skuColorSizeVo.getColor().isEmpty()){
            ColorEnum.fromString(skuColorSizeVo.getColor());
        }
        Date createTime=new Date();
        LoginUser loginUser=tokenService.getLoginUser(ServletUtils.getRequest());
        String createBy=loginUser.getUsername();
        SkuColorSize skuColorSize=new SkuColorSize(skuColorSizeVo);
        skuColorSize.setCreateBy(createBy);
        skuColorSize.setCreateTime(createTime);
        return skuColorSizeMapper.insertSkuColorSize(skuColorSize);
    }

    /**
     * 检查主键唯一
     * @param skuCode
     * @return true 已存在
     */
    @Override
    public boolean checkUnique(String skuCode) {
        return skuColorSizeMapper.checkUnique(skuCode) > 0;
    }

    @Override
    public String generateSkuCode(String productSn,String skuCode){
        String newSkuCode;
        if(!skuCode.isEmpty()){
            newSkuCode=skuCode;
        }
        else {
            newSkuCode = "kc"+ productSn;
        }
        if(checkUnique(newSkuCode)){
            newSkuCode=generateUUID();
        }
        return newSkuCode;
    }

    String generateUUID(){
        String id = "kc"+ UUID.randomUUID().toString().replace("-","").substring(0,3);
        if(checkUnique(id)){
            generateUUID();
        }
        return id;
    }
}
